package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.CommentsModel;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import com.kaizzi.data.jdl.service.UserService;
import lombok.Data;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/14
 */
@Component
public class PageData9 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){
        List<String> dateList = DateUtil.getDateList(1);
        List<String> transTime = DateUtil.getTransTime(dateList.get(0));

        List<String> ytdDay = DateUtil.getYtdDay();

        //性别
        List<TempBeen> genderData = getGenderData(transTime);
        List<TempBeen> genderData1 = getGenderData(ytdDay);
        //年龄
        List<TempBeen> ageData = getAgeData(transTime);
        List<TempBeen> ageData1 = getAgeData(ytdDay);
        //地区
        List<TempBeen> areaData = getAreaData(transTime);
        List<TempBeen> areaData1 = getAreaData(ytdDay);
        areaData.add(getOtherArea(genderData, areaData));
        areaData1.add(getOtherArea(genderData1,areaData1));
        areaData.sort(Comparator.comparing(TempBeen::getTempCount));
        areaData1.sort(Comparator.comparing(TempBeen::getTempCount));
        //vip
        List<TempBeen> vip = getVip(ytdDay);
        //text1
        String text1 = getText1(genderData, ageData);
        String text11 = getText1(genderData1, ageData1);
        //text2
        String text2 = getText2(areaData);
        String text21 = getText2(areaData1);
        //vipText
        String vipText = getVipText(vip);

        Page9 page9 = new Page9();
        page9.setTitle(LogicUtil.getTitle(model,9));
        page9.setWtdDataPeriod(DateUtil.getLastWeekDate());
        page9.setYtdDataPeriod(DateUtil.getFirstDay2LastSunday());
        page9.setWtdGender(genderData);
        page9.setWtdAge(ageData);
        page9.setWtdArea(areaData);
        page9.setWtdText1("本周新增"+text1);
        page9.setWtdText2("新增"+text2);

        page9.setYtdGender(genderData1);
        page9.setYtdAge(ageData1);
        page9.setYtdArea(areaData1);
        page9.setYtdText1("YTD的注册"+text11);
        page9.setYtdText2("YTD注册"+text21);

        page9.setVip(vip);
        page9.setVipText(vipText);

        return JsonUtil.serialize(page9);
    }

    private List<TempBeen> getGenderData(List<String> dateList){
        List<TempBeen> genderDistribute = crmMemberService.getGenderDistribute(dateList.get(0), dateList.get(1));
        List<TempBeen> tempBeens = new ArrayList<>();
        for (TempBeen tempBeen : genderDistribute) {
            if (tempBeen.getTempName().equals("未知") || tempBeen.getTempName().equals("女") || tempBeen.getTempName().equals("男")){
                tempBeens.add(tempBeen);
            }
        }
        return tempBeens;
    }

    private List<TempBeen> getAgeData(List<String> dateList){
        return crmMemberService.getAgeDistribute(dateList.get(0),dateList.get(1));
    }

    private List<TempBeen> getAreaData(List<String> dateList){
        return crmMemberService.getAreaDistribute(dateList.get(0),dateList.get(1));
    }

    private List<TempBeen> getVip(List<String> dateList){
        return crmMemberService.getVip(dateList.get(0),dateList.get(1));
    }

    private String getText1(List<TempBeen> gender,List<TempBeen> age){
        if (gender == null || gender.size() == 0 ) {
            return "";
        }
        age.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        String manRate = "0%";
        String womanRate = "0%";
        for (TempBeen been : gender) {
            if (been.getTempName().equals("男")){
                manRate = been.getTempData();
                continue;
            }
            if (been.getTempName().equals("女")){
                womanRate = been.getTempData();
            }
        }
        return "用户中'"+manRate+"'为男性用户，'"+womanRate+"'为女性用户；年龄以'"+age.get(0).getTempName()+"、"+age.get(1).getTempName()+"'为主；";
    }

    private String getText2(List<TempBeen> area){
        if (area == null || area.size() < 2){
            return "";
        }
        area.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        return "用户主要分布地区在'"+area.get(0).getTempName()+"、"+area.get(1).getTempName()+"、"+area.get(2).getTempName()+"'等地；";
    }

    private String getVipText(List<TempBeen> vip){
        vip.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        return vip.get(0).getTempName()+">"+vip.get(1).getTempName()+">"+vip.get(2).getTempName()+">"+vip.get(3).getTempName();
    }

    private TempBeen getOtherArea(List<TempBeen> list1,List<TempBeen> list2){
        //性别total-地区前十 = 其他
        double sum = list1.stream().mapToDouble(TempBeen::getTempCount).sum();
        double sum1 = list2.stream().mapToDouble(TempBeen::getTempCount).sum();
        double aa = sum - sum1;
        TempBeen tempBeen = new TempBeen();
        tempBeen.setTempName("其他");
        tempBeen.setTempCount((long)aa);
        return tempBeen;
    }

    @Data
    private static class Page9{
        private String title;
        private String wtdDataPeriod;
        private String ytdDataPeriod;

        private List<TempBeen> ytdGender;
        private List<TempBeen> ytdAge;
        private List<TempBeen> ytdArea;
        private String ytdText1;
        private String ytdText2;

        private List<TempBeen> wtdGender;
        private List<TempBeen> wtdAge;
        private List<TempBeen> wtdArea;
        private String wtdText1;
        private String wtdText2;

        private List<TempBeen> vip;
        private String vipText;
    }

//    @Data
//    private static class TempData9{
//        private String date;
//        private String count;
//    }
}
